﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using PH.Model.Webs;
namespace PH.Data.Webs
{
    public class PageCommand
    {
        public static PageCollection Gets(PageQuery sea)
        {
            var ret = new PageCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<Page> q = db.DataContext.Pages;
                if (sea != null)
                {
                    if (sea.CaseID > 0)
                        q = q.Where(p => p.CaseID == sea.CaseID);
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderBy(p => p.Pindex).ThenByDescending(p => p.ID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<PageModel, Page, PageCollection>(lst);
                    ret.TotalRecordsCount = n;
                }
            }
            return ret;
        }

        public static PageModel GetPage(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.Pages.FirstOrDefault(p => p.ID == id);
                    if (d != null)
                    {
                        var ret = Convertor.ConvertTo<PageModel, Page>(d);
                        return ret;
                    }
                }
            }
            return null;
        }

        public static PageModel CreateNewPage(PageModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = Convertor.ConvertFrom<Page, PageModel>(model);
                    db.DataContext.Pages.Add(d);
                    db.DataContext.SaveChanges();
                    if (string.IsNullOrEmpty(d.FriendUrl))
                        d.FriendUrl = string.Format("/Home/Article/{0}", d.ID);
                    db.DataContext.SaveChanges();

                    model.ID = d.ID;
                    return model;
                }
            }
            return null;
        }

        public static void UpdatePage(PageModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.Pages.FirstOrDefault(p => p.ID == model.ID);
                    if (d != null)
                    {
                        if (string.IsNullOrEmpty(model.Description))
                            model.Description = d.Description;
                        Convertor.UpdateValue(model, d);
                        db.DataContext.SaveChanges();
                    }
                }
            }
        }

        public static void DeletePage(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.Pages.FirstOrDefault(p => p.ID == id);
                    if (d != null)
                    {
                        if (d.PageType == 1)
                        {
                            db.DataContext.Pages.Remove(d);
                            db.DataContext.SaveChanges();
                        }
                    }
                }
            }
        }

        public static PageModel GetTopPage(int caseID)
        {
            if (caseID > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.Pages.OrderByDescending(p => p.ID).FirstOrDefault(p => p.CaseID == caseID);
                    if (d != null)
                    {
                        var ret = Convertor.ConvertTo<PageModel, Page>(d);
                        return ret;
                    }
                }
            }
            return null;
        }
    }
}
